﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using SecsEquip;

namespace EquipSecs300
{
    public partial class VariablesView : CMyFormEx
    {
        GEM_Variables Variables;
        public VariablesView()
        {
            m_pViewName = "变量";

            InitializeComponent();

            InitCtrl();

        }

        // 更新数据
        public override void UpdateData()
        {
            // 从工具里面获取          
            int nVID;
            List<VariableInfo> info = null;
            m_pSecs.VariableGetList(ref info);
            Variables = secsEquipHelper.Variables;

            if (info != null)
            {
                for (int i = 0; i < info.Count; i++)
                {
                    nVID = UtilitiesConvert.StringToInt32(info[i].VID);
                    AddDataToView(nVID, info[i].Name, info[i].Description, info[i].varClass,
                        info[i].Type, info[i].Value, info[i].Units);
                }
            }
        }

        // 获取当前选择的ID
        private int GetCurSeletID()
        {
            int nCEID = 0;
            if (listViewVariables.SelectedItems.Count != 0)
            {
                string strCEID = listViewVariables.SelectedItems[0].Text;
                nCEID = UtilitiesConvert.StringToInt32(strCEID);
            }
            return nCEID;
        }

        //初始化控件
        void InitCtrl()
        {
            listViewVariables.Columns.Add("ID", 100, HorizontalAlignment.Left);
            listViewVariables.Columns.Add("变量名称(var name)", 160, HorizontalAlignment.Left);
            listViewVariables.Columns.Add("描述(DES)", 420, HorizontalAlignment.Left);
            listViewVariables.Columns.Add("类", 50, HorizontalAlignment.Left);
            listViewVariables.Columns.Add("数值类型", 50, HorizontalAlignment.Left);
            listViewVariables.Columns.Add("值", 150, HorizontalAlignment.Left);
            listViewVariables.Columns.Add("单位", 50, HorizontalAlignment.Left);
        }

        // 增加到界面
        void AddDataToView(int ID, string varName, string description, string varClass,
            string ValueType, string Value, string Units)
        {
            listViewVariables.BeginUpdate();

            ListViewItem lvi = new ListViewItem();
            lvi.Text = ID.ToString();

            lvi.SubItems.Add(varName);
            lvi.SubItems.Add(description);
            lvi.SubItems.Add(varClass);
            lvi.SubItems.Add(ValueType);
            lvi.SubItems.Add(Value);
            lvi.SubItems.Add(Units);

            listViewVariables.Items.Add(lvi);

            listViewVariables.EndUpdate();
        }


        // 增加到SECS
        void AddDataToSECS(int ID, string varName, string description, string varClass,
            string ValueType, string Value, string Units)
        {
            // 添加到Secs tool
            m_pSecs.VariableAdd(ID, varName, description, ValueType, Value, Units);
        }

        // 更新list数据
        void UpdateLisData()
        {
            listViewVariables.BeginUpdate();

            ListViewItem lvi = new ListViewItem();

            lvi.ImageIndex = 0;     //通过与imageList绑定，显示imageList中第i项图标

            lvi.Text = "subitem";

            lvi.SubItems.Add("第2列");

            listViewVariables.Items.Add(lvi);

            listViewVariables.EndUpdate();

        }

        private void listViewVariables_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewVariables.SelectedItems.Count == 0)
            {
                return;
            }

            string ID = listViewVariables.SelectedItems[0].Text;
            string Name = listViewVariables.SelectedItems[0].SubItems[1].Text;
            string Description = listViewVariables.SelectedItems[0].SubItems[2].Text;
            string VarClass = listViewVariables.SelectedItems[0].SubItems[3].Text;
            string Type = listViewVariables.SelectedItems[0].SubItems[4].Text;
            string Value = listViewVariables.SelectedItems[0].SubItems[5].Text;
            string Units = listViewVariables.SelectedItems[0].SubItems[6].Text;

            try
            {
                // 获取最新值
                int varID = int.Parse(ID);
                RcResult rc = m_pSecs.VariableGet(varID, ref Value);

                if (rc == 0)
                {
                    listViewVariables.SelectedItems[0].SubItems[5].Text = Value;
                    ShowValue(ID, Name, Description, VarClass, Type, Value, Units);
                }
            }
            catch
            {
            }

        }

        void ShowValue(string ID, string Name, string Description,
            string VarClass, string Type, string CurrentValue, string Units)
        {
            labelID.Text = "ID: " + ID;
            labelName.Text = "名称: " + Name;
            labelDes.Text = "描述: " + Description;
            labelClass.Text = "类: " + VarClass;
            labelType.Text = "数值类型: " + Type;
            labelUnits.Text = "单位: " + Units;

            textBoxCurrValue.Text = CurrentValue;
        }

        // 设置值
        private void buttonSetValue_Click(object sender, EventArgs e)
        {
            // 设置
            if (listViewVariables.SelectedItems.Count >= 1)
            {
                string VID = listViewVariables.SelectedItems[0].Text;
                string newValue = textBoxCurrValue.Text;
                int nVID = UtilitiesConvert.StringToInt32(VID);
                m_pSecs.VariableSet(nVID, newValue);

                //查询
                RcResult rc = m_pSecs.VariableGet(nVID, ref newValue);
                if (rc == 0)
                {
                    listViewVariables.SelectedItems[0].SubItems[5].Text = newValue;
                }
            }
        }

    }
}
